﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using wepoverkefni5.Models;
using System.Configuration;
using System.Web.Mvc;


namespace wepoverkefni5.Models
{
    /// <summary>
    /// Data repository containing all methods for database procedures
    /// </summary>
    public class DataRepository
    {
        MusicDBDataContext m_db = new MusicDBDataContext();

        /*UPDATE METHODS*/
        public void updateSongSource(int id, String source, String name, String album, String artist, String Genre, String duration)
        {
            var result = GetSongById(id);
            if (result != null)
            {
                result.songSource = source;
                if (name != null)
                {
                    result.songName = name;
                }
                if (album != null)
                {
                    result.songAlbum = album;
                }
                if (artist != null)
                {
                    result.songArtist = artist;
                }
                if (Genre != null)
                {
                    result.songGenre = Genre;
                }
                if (duration != null)
                {
                    result.songLength = duration;
                }
                m_db.SubmitChanges();
            }
        }

        /*GET METHODS*/
        public IQueryable<Song> GetSongBySearch(String search)
        {
            var result = from item in m_db.Songs
                         where item.songName.Contains(search)
                         || item.songArtist.Contains(search)
                         || item.songAlbum.Contains(search)
                         || item.songGenre.Contains(search)
                         select item;
            return result;
        }

        public Song GetSongById(int id)
        {
            var result = m_db.Songs.Where(x => x.Id == id).SingleOrDefault();
            return result;
        }

        public IQueryable<Playlist> GetAllPlaylists()
        {
            var result = m_db.Playlists.Select(x => x);

            return result;
        }

        public IQueryable<Song> GetPlaylistSongs(int id)
        {
            var result = from item in m_db.Songs
                         from item2 in m_db.PlaylistSongs
                         where item2.playlistId == id
                         && item.Id == item2.songId
                         select item;
            return result;
        }

        public IEnumerable<PlaylistSong> GetPlaylistRecordsById(int id)
        {
            var result = from item in m_db.PlaylistSongs
                         where item.songId == id
                         select item;
            return result;
        }

        public IQueryable<PlaylistSong> getPlaylistSongsByPlaylistId(int id)
        {
            var result = from item in m_db.PlaylistSongs
                         where item.playlistId == id
                         select item;
            return result;
        }

        public IQueryable<Song> GetAllSongs()
        {
            var result = m_db.Songs.Select(x => x);
            return result;
        }

        public PlaylistSong getPlaylistItem(int id)
        {
            var result = m_db.PlaylistSongs.Where(x => x.songId == id).Single();
            return result;
        }

        public Playlist getSinglePlaylist(int id)
        {
            var result = m_db.Playlists.Where(x => x.Id == id).Single();
            return result;
        }

      
        /*ADD METHODS*/
        public void addSong(Song song)
        {
            m_db.Songs.InsertOnSubmit(song);
            m_db.SubmitChanges();
        }

        public void addNewPlaylist(Playlist name)
        {
            m_db.Playlists.InsertOnSubmit(name);
            m_db.SubmitChanges();
        }

        public PlaylistSong GetPlayListSong(int sondId, int playlistId)
        {
            var result = (from item in m_db.PlaylistSongs
                          where item.songId == sondId
                          && item.playlistId == playlistId
                          select item).SingleOrDefault();
            return result;
        }

        public void addNewPlaylistSong(PlaylistSong song)
        {
            var result = GetPlayListSong(song.songId, song.playlistId);
            if (result == null)
            {
                m_db.PlaylistSongs.InsertOnSubmit(song);
                m_db.SubmitChanges();
            }
        }

        /*DELETE METHODS*/
        public void deletePlaylistSong(PlaylistSong song)
        {
            m_db.PlaylistSongs.DeleteOnSubmit(song);
            m_db.SubmitChanges();
        }

        public void deletePlaylistSongs(IQueryable<PlaylistSong> listSongs)
        {
            m_db.PlaylistSongs.DeleteAllOnSubmit(listSongs);
            m_db.SubmitChanges();
        }
        public void deletePlaylist(Playlist list)
        {
            m_db.Playlists.DeleteOnSubmit(list);
            m_db.SubmitChanges();
        }

        public void DeleteSong(Song song)
        {
            var playlistRecords = GetPlaylistRecordsById(song.Id);
            if(playlistRecords != null)
            {
                m_db.PlaylistSongs.DeleteAllOnSubmit(playlistRecords);
            }
            m_db.Songs.DeleteOnSubmit(song);
            m_db.SubmitChanges();
        }
    }
}